﻿#region Copyright (c) 2003 - 2008, Andreas Mueller
/////////////////////////////////////////////////////////////////////////////////////////
// 
// Copyright (c) 2003 - 2008, Andreas Mueller.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
//    Andreas Mueller - initial API and implementation
//
// 
// This software is derived from software bearing the following
// restrictions:
// 
// Copyright (c) 1994
// Hewlett-Packard Company
// 
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  Hewlett-Packard Company makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied warranty.
// 
// 
// Copyright (c) 1996,1997
// Silicon Graphics Computer Systems, Inc.
// 
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation.  Silicon Graphics makes no
// representations about the suitability of this software for any
// purpose.  It is provided "as is" without express or implied warranty.
// 
// 
// (C) Copyright Nicolai M. Josuttis 1999.
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
// 
/////////////////////////////////////////////////////////////////////////////////////////
#endregion


using NStl.Collections;
using NStl.Exceptions;
using NStl.Linq;
using NUnit.Framework;
using System.Collections.Generic;
using System.Collections;
using System;
using NUnit.Framework.SyntaxHelpers;


namespace NStl.NUnit.ContainerFixture.Vector
{
    [TestFixture, Obsolete]
    public class VectorFixture
    {
        [Test]
        public void ConstructNonEmpty()
        {
            const int initialCount = 5;
            Vector<int> v = new Vector<int>(initialCount);

            CollectionAssert.IsNotEmpty(v);
            Assert.AreEqual(initialCount, v.Count);

            ICollection expected = new[] {0, 0, 0, 0, 0};

            CollectionAssert.AreEqual(expected, v);
        }
        [Test]
        public void ConstructEmpty()
        {
            Vector<int> v = new Vector<int>();

            CollectionAssert.IsEmpty(v);
        }

        [Test]
        public void ConstructWithICollectionT()
        {
            int[] src = new[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
            Vector<int> v = new Vector<int>((ICollection<int>)src);

            CollectionAssert.AreEqual(src, v);
        }

        [Test]
        public void ConstructWithIEnumerable()
        {
            int[] src = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Vector<int> v = new Vector<int>((IEnumerable)src);

            CollectionAssert.AreEqual(src, v);
        }

        [Test]
        public void ConstructWithParams()
        {
            int[] expected = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Vector<int> v = new Vector<int>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

            CollectionAssert.AreEqual(expected, v);
        }

        [Test]
        public void ConstructWithIterators()
        {
            int[] expected = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            Vector<int> v = new Vector<int>(expected.Begin(), expected.End());

            CollectionAssert.AreEqual(expected, v);
        }

        [Test]
        public void Back()
        {
            Vector<int> vector1 = new Vector<int>();
            vector1.PushBack(1);
            vector1.PushBack(2);
            Assert.AreEqual(2, vector1.Back());
        }
        [Test]
        public void PushBack()
        {
            Vector<int> vector1 = new Vector<int>();
            vector1.PushBack(1);
            Assert.AreEqual(1, vector1.Back());
            Assert.That(vector1.Count, Is.EqualTo(1));
        }
        [Test]
        public void PushBackRange()
        {
            Vector<int> vector1 = new Vector<int>(0, 1, 2, 3, 4);
            IEnumerable<int> ints = new[] {5, 6, 7, 8, 9};
            vector1.PushBackRange(ints);

            ICollection expected = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            Assert.That(vector1, Is.EqualTo(expected));
        }
        private static void NeverCalled(object o)
        {
            NeverCalled(o);
        }
        [Test]
        [ExpectedException(typeof(ContainerEmptyException))]
        public void BackOnEmptyContainer()
        {
            Vector<int> vector1 = new Vector<int>();

            NeverCalled(vector1.Back());
        }
        [Test]
        public void Clear()
        {
            Vector<int> vector1 = new Vector<int>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

            Assert.AreEqual(10, vector1.Count);
            vector1.Clear();

            CollectionAssert.IsEmpty(vector1);
        }

        [Test]
        public void Empty()
        {

            Vector<int> vector1 = new Vector<int>();
            Assert.IsTrue(vector1.Empty);
            
            for(int i = 0; i < 10; ++i)
                vector1.PushBack(i);

            CollectionAssert.IsNotEmpty(vector1);
            Assert.IsFalse(vector1.Empty);
            vector1.Clear();

            CollectionAssert.IsEmpty(vector1);
            Assert.IsTrue(vector1.Empty);
        }

        [Test]
        public void Erase()
        {
            Vector<int> vector1 = new Vector<int>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

            Assert.AreEqual(10, vector1.Count);
            Assert.AreEqual(9, vector1.Back());

            // erase the last
            vector1.Erase(vector1.End() - 1);

            Assert.AreEqual(9, vector1.Count);
            Assert.AreEqual(8, vector1.Back());

            vector1.Erase(vector1.Begin() + 1, vector1.Begin() + 3);

            ICollection expected = new[] {0, 3, 4, 5, 6, 7, 8};
            CollectionAssert.AreEqual(expected, vector1);
        }

        [Test]
        public void Front()
        {
            Vector<int> vector1 = new Vector<int>();
            vector1.PushBack(1);

            Assert.AreEqual(1, vector1.Front());
        }

        [Test]
        [ExpectedException(typeof(ContainerEmptyException))]
        public void FrontOnEmptyContainer()
        {
            Vector<int> vector1 = new Vector<int>();
            NeverCalled(vector1.Front());
        }

        [Test]
        public void Indexer()
        {
            Vector<int> vector1 = new Vector<int>();
            vector1.PushBack(2);
            Assert.AreEqual(2, vector1[0]);
        }
        [Test]
        public void IndexerWrite()
        {
            Vector<int> vector1 = new Vector<int>(42);
            vector1[0] = 24;
            Assert.AreEqual(24, vector1[0]);
        }

        [Test]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void IndexerOutOfRange()
        {
            Vector<int> vector1 = new Vector<int>();

            NeverCalled(vector1[42]);
        }

        [Test]
        public void InsertBegin()
        {
            Vector<int> v = new Vector<int>(0,1,2,3);
            Vector<int>.Iterator insertAt = v.Begin();
            Vector<int>.Iterator insertedAt = 
                v.Insert(insertAt, 2);

            Assert.AreEqual(insertAt, insertedAt);

            ICollection expected = new[] {2, 0, 1, 2, 3};

            CollectionAssert.AreEqual(expected, v);
        }
        [Test]
        public void Insert()
        {
            Vector<int> v = new Vector<int>(0, 1, 2, 3);
            Vector<int>.Iterator insertAt = v.Begin() + 2;

            Vector<int>.Iterator insertedAt =
                v.Insert(insertAt, 2);
            Assert.AreEqual(insertAt, insertedAt);

            ICollection expected = new[] { 0, 1, 2, 2, 3 };

            CollectionAssert.AreEqual(expected, v);
        }
        [Test]
        public void InsertWithCount()
        {
            Vector<int> v = new Vector<int>(0, 1, 2, 3);
            Vector<int>.Iterator insertAt = v.Begin() + 2;

            v.Insert(insertAt, 2, 42);

            ICollection expected = new[] { 0, 1, 42, 42, 2, 3 };

            CollectionAssert.AreEqual(expected, v);
        }
        [Test]
        public void InsertEnd()
        {
            Vector<int> v = new Vector<int>(0, 1, 2, 3);
            Vector<int>.Iterator insertAt = v.End();

            Vector<int>.Iterator insertedAt = 
                v.Insert(insertAt, 2);

            Assert.AreEqual(insertAt, insertedAt);
            Assert.AreNotEqual(v.End(), insertedAt);// check this :-)

            ICollection expected = new[] { 0, 1, 2,  3, 2 };

            CollectionAssert.AreEqual(expected, v);
        }
        [Test]
        public void ReverseIterators()
        {
            Vector<int> src = new Vector<int>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9 );
            Vector<int> target = new Vector<int>(src.Count);

            Algorithm.Copy(src.RBegin(), src.REnd(), target.Begin());
            ICollection expected = new[] {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
            CollectionAssert.AreEqual(expected, target);
        }
        [Test]
        public void Size()
        {
            Vector<int> v = new Vector<int>();
            Assert.AreEqual(0, v.Count);
            v.PushBack(2);

            Assert.AreEqual(1, v.Count);
            v.PushBack(2);
            Assert.AreEqual(2, v.Count);
            v.PopBack();
            Assert.AreEqual(1, v.Count);
        }

        [Test]
        public void ReserveCapacity()
        {
            Vector<int> v = new Vector<int>(1, 2);

            int doubleCapacity = v.Capacity() * 2;
            v.Reserve(doubleCapacity);
            Assert.AreEqual(2, v.Count);
            Assert.AreEqual(v.Capacity(), doubleCapacity);
            
            int end = (doubleCapacity - 1) - 2;
            for (int i = 0; i <= end; i++)
                v.PushBack(i);

            Assert.AreEqual(v.Capacity(), v.Count);
            v.PushBack(42);
            Assert.AreEqual(v.Capacity(), (doubleCapacity * 2));
        }


        [Test]
        public void Resize()
        {
            Vector<int> v = new Vector<int>();
            v.PushBack(2);
            Assert.AreEqual(1, v.Count);
            v.Resize(5, 0);

            Assert.AreEqual(5, v.Count);
            Assert.IsTrue(v[0] == 2);
            Assert.IsTrue(v[1] == 0);
            Assert.IsTrue(v[2] == 0);
            Assert.IsTrue(v[3] == 0);
            Assert.IsTrue(v[4] == 0);
           
            v.Resize(3);
            Assert.AreEqual(3, v.Count);
            Assert.IsTrue(v[0] == 2);
            Assert.IsTrue(v[1] == 0);
            Assert.IsTrue(v[2] == 0);
        }
        [Test]
        public void ResizeWithShrink()
        {
            const int minCapacity = 16;

            Vector<int> v = new Vector<int>(55);
            Assert.AreEqual(55, v.Count);

            v.Resize(minCapacity + 1);

            Assert.AreEqual(minCapacity + 1, v.Count);
            Assert.AreEqual(minCapacity + 1, v.Capacity());
        }
        [Test]
        public void PopBack()
        {
            Vector<int> v = new Vector<int>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

            Assert.AreEqual(10, v.Count);
            Assert.AreEqual(9, v.Back());
            Assert.AreEqual(0, v.Front());
            
            v.PopBack();
            Assert.AreEqual(8, v.Back());
        }
        [Test]
        [ExpectedException(typeof(ContainerEmptyException))]
        public void PopBackOnEmptyContainer()
        {
            Vector<int> v = new Vector<int>();
            v.PopBack();
        }
        [Test]
        public void Swap()
        {
            int[] expected1 = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[] expected2 = new[] { 0, 2, 4, 6, 8, 10, 12};

            Vector<int> v1 = new Vector<int>(expected1);
            Vector<int> v2 = new Vector<int>(expected2);

            v1.Swap(v2);

            CollectionAssert.AreEqual(expected1, v2);
            CollectionAssert.AreEqual(expected2, v1);
        }

        [Test]
        public void ImplementsIRange()
        {
            Vector<int> v = new Vector<int>(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
            IRange<int> r = v;

            Assert.AreEqual(r.Begin(), v.Begin());
            Assert.AreEqual(r.End(), v.End());
        }
        [Test]
        public void ImplementsIInsertable()
        {
            Vector<int> v = new Vector<int>(0, 1, 2, 3);
            IInsertable<int, Vector<int>.Iterator> insertable = v;

            Vector<int>.Iterator insertAt = v.Begin() + 2;

            Vector<int>.Iterator insertedAt =
                insertable.Insert(insertAt, 2);
            Assert.AreEqual(insertAt, insertedAt);

            ICollection expected = new[] { 0, 1, 2, 2, 3 };

            CollectionAssert.AreEqual(expected, v);
        }
    }
}
